﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;

using KF.DHLModel;

namespace DHLPricing
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();

            //KF.DHLBL.ZoneInfo zoneBL = new KF.DHLBL.ZoneInfo();
            //KF.DHLBL.PriceInfo priceBL = new KF.DHLBL.PriceInfo();
            //zoneInfos = zoneBL.GetZoneInfo();
            //priceInfos = priceBL.GetPriceInfo();

            openFileDialog1.Filter = "excel文件|*.xls";

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //KF.DHLBL.Prcing p = new KF.DHLBL.Prcing();
            //double price = p.CalcPrice(priceInfos, 5, 0.51, KF.DHLModel.ItemType.File, KF.DHLModel.CustomerType.Normal);
            //MessageBox.Show(price.ToString());


        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string message = "";
            List<KF.DHLModel.PricingInfo> infos = LoadPricingInfoFromExcel(txtFilePath.Text, ref message);
            if (infos == null)
            {
                MessageBox.Show(message);
                return;
            }

            //KF.DHLBL.Prcing p = new KF.DHLBL.Prcing();
            Service.DHLPricingSoapClient client = new Service.DHLPricingSoapClient();
            String msg = "";
            foreach (KF.DHLModel.PricingInfo info in infos)
            {
                try
                {
                    info.Price = client.CalcPrice(info.Zone, info.RealWeight, (Service.ItemType)(int)info.ItemType, (Service.CustomerType)(int)info.Type);
                }
                catch (Exception ex)
                {
                    msg += string.Format("处理订单{0}时:发生错误[{1}],请检查数据!", info.OrderNo, ex.Message);
                }
            }
            if (msg != "")
            {
                MessageBox.Show(msg);
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "excel文件|*.xls";//自动保存文件类型

            if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;//如果用户没有选择确定，则不进行以后操作
            }



            //创建xls文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建sheet表单，默认名称为sheet1
            HSSFSheet sheet = workbook.CreateSheet("用户数据");
            //创建 用户数据 表格的行，并设定第一行的表头
            HSSFRow rowTitle = sheet.CreateRow(0);
            //第1行 第1列为“序号” 第2列为“用户名” 第3列为“密码”
            rowTitle.CreateCell(0, HSSFCellType.STRING).SetCellValue("订单号");
            rowTitle.CreateCell(1, HSSFCellType.STRING).SetCellValue("区域");
            rowTitle.CreateCell(2, HSSFCellType.STRING).SetCellValue("重量");

            rowTitle.CreateCell(3, HSSFCellType.STRING).SetCellValue("货件类型");
            rowTitle.CreateCell(4, HSSFCellType.STRING).SetCellValue("报价类型");

            rowTitle.CreateCell(5, HSSFCellType.STRING).SetCellValue("价格");


            int rowNum = 1;//第2行内容，第1已被表头“用户名”和“密码”占用
            foreach (KF.DHLModel.PricingInfo info in infos)
            {

                HSSFRow rowContent = sheet.CreateRow(rowNum);//创建内容行

                //对第1行对应的单元格分别设定单元格内容属性和填充对应的值
                rowContent.CreateCell(0, HSSFCellType.STRING).SetCellValue(info.OrderNo);
                rowContent.CreateCell(1, HSSFCellType.STRING).SetCellValue(info.Zone);
                rowContent.CreateCell(2, HSSFCellType.STRING).SetCellValue(info.RealWeight);
                rowContent.CreateCell(3, HSSFCellType.STRING).SetCellValue(info.ItemType.ToString());
                rowContent.CreateCell(4, HSSFCellType.STRING).SetCellValue(info.Type.ToString());
                rowContent.CreateCell(5, HSSFCellType.STRING).SetCellValue(info.Price);
                rowNum++;//动态增加行号
            }
            MessageBox.Show(rowNum - 1 + "条数据导出成功");//实际导出的数据比定义的行号少1


            //写入excel文件
            using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);//把流写入文件
            }
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK && openFileDialog1.CheckFileExists && openFileDialog1.CheckPathExists)
            {
                txtFilePath.Text = openFileDialog1.FileName;
            }
        }

        public static List<KF.DHLModel.PricingInfo> LoadPricingInfoFromExcel(string fileName, ref string msg)
        {
            HSSFWorkbook workbook = null;
            msg = "";
            try
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    workbook = new HSSFWorkbook(fileStream);
                }
            }
            catch(Exception message)
            {
                msg = "文件读取发生问题，" + message;
            }
            if (msg != "")
            {
                return null;
            }

            HSSFSheet sheet1 = workbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet1.GetRowEnumerator();

            int i = 0;
            DateTime now = DateTime.Now;
            List<KF.DHLModel.PricingInfo> infos = new List<KF.DHLModel.PricingInfo>();



            while (rows.MoveNext())
            {

                if (i > 0)//
                {
                    HSSFRow row = (HSSFRow)rows.Current;
                    if (row.GetCell(0).CellType != HSSFCellType.BLANK)
                    {
                        //if (row.GetCell(0).NumericCellValue != 0)
                        {
                            KF.DHLModel.PricingInfo info = new KF.DHLModel.PricingInfo();
                            info.OrderNo = row.GetCell(0).ToString();
                            int zoneid = 0;
                            try
                            {
                                zoneid = int.Parse(row.GetCell(1).ToString());
                            }
                            catch
                            {
                                zoneid = -1;
                            }
                            if (zoneid < 1 || zoneid > 11)
                            {
                                zoneid = -1;
                            }

                            if (zoneid == -1)
                            {
                                msg = string.Format("第{0}行:区域[ {1} ]不准确!", i + 1, row.GetCell(1));
                                return null;
                            }
                            info.Zone = zoneid;

                            double weight = 0.0;
                            try
                            {
                                weight = double.Parse(row.GetCell(2).ToString());
                            }
                            catch
                            {
                                weight = -1;
                            }
                            if (weight < 0 || weight > 300)
                            {
                                weight = -1;
                            }
                            if (weight == -1)
                            {
                                msg = string.Format("第{0}行:重量[ {1} ]不准确!", i + 1, row.GetCell(2));
                                return null;
                            }

                            info.RealWeight = weight;

                            String packgeTypeDesc = "";
                            try
                            {
                                packgeTypeDesc = row.GetCell(3).ToString();
                            }
                            catch
                            {
                                packgeTypeDesc = "-1";
                            }
                            if (packgeTypeDesc.Trim().ToUpper() != "DOC" && packgeTypeDesc.Trim().ToUpper() != "NO")
                            {
                                packgeTypeDesc = "-1";
                            }

                            if (packgeTypeDesc == "-1")
                            {
                                msg = string.Format("第{0}行:货件类型[ {1} ]不准确!", i + 1, row.GetCell(3));
                                return null;
                            }
                            info.ItemType = packgeTypeDesc.Trim().ToUpper() == "DOC" ? KF.DHLModel.ItemType.DOC : KF.DHLModel.ItemType.NO;

                            String PriceType = "";
                            try
                            {
                                PriceType = row.GetCell(4).ToString();
                            }
                            catch
                            {
                                PriceType = "-1";
                            }
                            if (PriceType.Trim().ToUpper() != "VIP" && PriceType.Trim().ToUpper() != "NO")
                            {
                                PriceType = "-1";
                            }
                            if (PriceType == "-1")
                            {
                                msg = string.Format("第{0}行:报价类型[ {1} ]不准确!", i + 1, row.GetCell(4));
                                return null;
                            }
                            info.Type = PriceType.Trim().ToUpper() == "VIP" ? KF.DHLModel.CustomerType.VIP : KF.DHLModel.CustomerType.NO;

                            infos.Add(info);
                        }
                    }
                }

                i++;
            }

            return infos;
        }

    }
}
